package com.example.boos.login;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.example.boos.R;
import com.example.boos.pojo.Boss;


public class RegisterFragment extends Fragment {
    private TextView userImageStr;
    private Button register;
    private ImageView userImage;
    private EditText userName;
    private EditText userTelephone;
    private EditText userPassword;
    private EditText userEmail;
    private EditText code;
    private Button getCode;
    private ImageView reg_back;
    private Boss boss = new Boss();

    private RegisterFragmentCallBack callBack;

    public RegisterFragment(RegisterFragmentCallBack callBack){
        this.callBack = callBack;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register,container,false);
        // 获取到 toolbar
        Toolbar toolbar =(Toolbar) view.findViewById(R.id.reg_toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 设置监听器
        listener();
    }

    private void listener(){
        reg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("测试Register","测试返回");
                getParentFragmentManager().popBackStack();
            }
        });
        userImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.changeUserImage();
            }
        });
        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = userEmail.getText().toString();
                if(email == null || email.equals("")){
                    Toast.makeText(getContext(),"请输入邮箱",Toast.LENGTH_SHORT).show();
                    return ;
                }
                callBack.getVerifyCode(v,email);
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userImageStr.getText().toString() != null){
                    boss.setImage(userImageStr.getText().toString());
                }
                boss.setBossName(userName.getText().toString());
                boss.setTelephone(userTelephone.getText().toString());
                boss.setPassword(userPassword.getText().toString());
                boss.setEmail(userEmail.getText().toString());
                boss.setMoney(0f);
                callBack.register(boss,code.getText().toString());
            }
        });
    }

    private void initView(View view){
        reg_back = view.findViewById(R.id.reg_back);
        register = view.findViewById(R.id.reg_register);
        userImage = view.findViewById(R.id.reg_userImage);
        userName = view.findViewById(R.id.reg_userName);
        userTelephone = view.findViewById(R.id.reg_userTelephone);
        userPassword = view.findViewById(R.id.reg_userPassword);
        userEmail = view.findViewById(R.id.reg_userEmail);
        code = view.findViewById(R.id.reg_code);
        getCode = view.findViewById(R.id.reg_getCode);
        userImageStr = view.findViewById(R.id.userImageStr);
    }
}
